﻿/**
 * 
 */
package org.mspring.mlog.utils;

import javax.servlet.http.HttpServletRequest;

import org.mspring.mlog.core.ServiceFactory;
import org.mspring.mlog.entity.Option;
import org.mspring.mlog.entity.SysUser;
import org.mspring.mlog.web.common.OptionKeys;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.view.freemarker.ExtendsFreeMarkerViewResolver;

/**
 * @author fairchildy
 * @since 2012-8-16
 * @Description
 * @TODO
 */
public class SkinUtils {
    /**
     * 获取主题所在URL
     * 
     * @param skin
     * @param request
     * @return
     */
    public static String getTemplateUrl(HttpServletRequest request) {
    	String userName = (String) request.getAttribute("userName");
    	SysUser user = ServiceFactory.getUserService().getUserByName(userName);
    	
        Option option = new Option();
        if (null != user) {
            Long userId = user.getUSER_ID();
            option.setuSER_ID(userId);
        } 
        // TODO 多用户时
        option.setName(OptionKeys.CURRENT_SKIN);
        String skinFolder = ExtendsFreeMarkerViewResolver.getSkinfolder();
        StringBuffer template_url = new StringBuffer(request.getContextPath());
        if (!StringUtils.startsWith(skinFolder, "/")) {
            template_url.append("/");
        }
        template_url.append(skinFolder);
        if (!StringUtils.endsWith(skinFolder, "/")) {
            template_url.append("/");
        }
        // TODO 单个用不能使用自己的皮肤
        
//        template_url.append(userName);
//        template_url.append("/");
        String skin = getSkin(option);
        template_url.append(skin);
        request.setAttribute(OptionKeys.CURRENT_SKIN, skin);
        option.setName("blogname");
        String title = ServiceFactory.getOptionService().getOption(option); // 频道名称
        request.setAttribute("blogname", title);
        option.setName("blogurl");
        String blogurl = ServiceFactory.getOptionService().getOption(option); // 频道名称
        request.setAttribute("blogurl", blogurl);
        option.setName("notice");
        String notice = ServiceFactory.getOptionService().getOption(option); // 频道名称
        request.setAttribute("notice", notice);
        
        return template_url.toString();
    }

    public static String getSkin(Option option) {
        String skin = ServiceFactory.getOptionService().getOption(option);
        if (StringUtils.isBlank(skin)) {
            // skin = "default";
        	// TODO　现在没有default
        	skin = "garland";
        }
        return skin;
    }
}
